<?php

namespace app\admin\controller\signup;

use app\common\controller\Backend;
use app\common\model\Group;

/**
 * 活动报名打卡管理
 *
 * @icon fa fa-circle-o
 */
class Signup extends Backend
{

    /**
     * Signup模型对象
     * @var \app\admin\model\signup\Signup
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\signup\Signup;
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['activity', 'user'])
                ->field('signup.id, sum(signup.number) as number, sum(signup.save_number) as save_number, activity_id, user_id, max(signup.createtime) as createtime')
                ->where($where)
                ->order('number desc, createtime desc')
                ->group('activity_id, user_id')
                ->paginate($limit)->each(function (&$value, $index) {
                    // 我的组别
                    $value['myGroup'] = Group::instance()->where(['id' => $value['user']['city_group_id'], 'status' => 1])->value('group_name');
                    // 我的全国排名
                    $myRanking = collection($this->model->alias('t1')
                        ->field('t1.user_id, sum(t1.number) as number, t2.city_group_id, t3.group_name')
                        ->join('user t2', 't1.user_id = t2.id', 'LEFT')
                        ->join('group t3', 't2.city_group_id = t3.id', 'LEFT')
                        ->where(['t1.activity_id' => $value['activity_id']])
                        ->order('number desc, t1.createtime asc')
                        ->group('t1.user_id')
                        ->select())
                        ->toArray();
                    $nationwideRanking = 0;
                    foreach ($myRanking as $key_ => $value_) {
                        $user_id = $value['user_id'];
                        if ($value_['user_id'] == $user_id) {
                            $nationwideRanking = ($key_ + 1);
                        }
                    }
                    $value['nationwideRanking'] = $nationwideRanking; // 我的全国排名
                    // todo 获取所有用户的打卡数量分组排名
                    $userGroupRanking = collection($this->model
                        ->alias('t1')
                        ->field('sum(t1.number) as number, t2.city_group_id, t3.group_name')
                        ->join('user t2', 't1.user_id = t2.id', 'LEFT')
                        ->join('group t3', 't2.city_group_id = t3.id', 'LEFT')
                        ->where(['t1.activity_id' => $value['activity_id']])
                        ->order('number desc, t1.createtime asc')
                        ->group('t2.city_group_id')
                        ->select())
                        ->toArray();
                    $groupRanking = 0;
                    foreach ($userGroupRanking as $keys => $values) {
                        $city_group_id = $value['user']['city_group_id'];
                        if ($values['city_group_id'] == $city_group_id) {
                            $groupRanking = ($keys + 1);
                        }
                    }
                    $value['groupRanking'] = $groupRanking; // todo 我的组排名
                });

            foreach ($list as $row) {
                $row->getRelation('activity')->visible(['id', 'title', 'image']);
                $row->getRelation('user')->visible(['id', 'nickname', 'mobile']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

}
